package com.service.business.support;

import com.alibaba.fastjson.JSONObject;
import com.service.dao.model.SysBorrowConfig;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * <dl>
 * <dt>back-end</dt>
 * <dd>Description:</dd>
 * <dd>Copyright: Copyright (C) 2015</dd>
 * <dd>Company: </dd>
 * <dd>CreateDate: 2019年11月12日</dd>
 * </dl>
 *
 * @author
 */
@Component
public class BorrowHelp {

    @Autowired
    private MemCache memCache;

    private static String BORROW_CONFIG = "borrow:config:";

    /**
     * 购物车:算力套餐
     *
     * @param sysBorrowConfig
     * @param coinId
     */
    public void addBorrowConfigCache(SysBorrowConfig sysBorrowConfig, Integer coinId) {
        String jsonObject = JSONObject.toJSONString(sysBorrowConfig);
        memCache.setNoExpire(BORROW_CONFIG + coinId, jsonObject);
    }

    public SysBorrowConfig getBorrowConfigCache(Integer coinId) {
        String json = memCache.get(BORROW_CONFIG + coinId);
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        return JSONObject.parseObject(json, SysBorrowConfig.class);
    }

    /**
     * 删除配置缓存
     * @param coinId
     */
    public void removeBorrowConfigCache(Integer coinId){
        memCache.delete(BORROW_CONFIG + coinId);
    }



}
